package com.novel.app.view.main.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.dongdxiang.framework.http.ImageLoader;
import com.novel.app.R;
import com.novel.app.utils.BaseViewHolder;
import com.novel.app.view.main.MainActivity;
import com.novel.app.view.main.bean.BookracksEntity;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by d_xxiang on 2016/12/14.
 */
public class Adpter extends ArrayAdapter<BookracksEntity> {
    private LayoutInflater inflater;
    private Context context;
    private boolean isEdit = false;
    private boolean selectAll = false;
    private List<String> checksList = new ArrayList<>();
    private MainActivity.CheckedListener listener;
    public Adpter(Context context, MainActivity.CheckedListener listener) {
        super(context, android.R.layout.simple_list_item_1);
        inflater = LayoutInflater.from(context);
        this.context = context;
        this.listener = listener;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        InfoHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_shujia, null);
            holder = new InfoHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (InfoHolder) convertView.getTag();
        }
        holder.populateView(position, getItem(position));
        return convertView;
    }

    public void isEdit(boolean isEdit){
        this.isEdit = isEdit;
    }
    class InfoHolder extends BaseViewHolder<BookracksEntity> {
        private ImageView img;
        private TextView name;
        private TextView dsc;
        private TextView data;
        private TextView sum_info;
        private CheckBox checkbox;
        public InfoHolder(View view) {
            super(view);
            img = (ImageView) view.findViewById(R.id.img);
            name = (TextView) view.findViewById(R.id.name);
            dsc = (TextView) view.findViewById(R.id.dsc);
            data = (TextView) view.findViewById(R.id.data);
            sum_info = (TextView) view.findViewById(R.id.sum_info);
            checkbox = (CheckBox) view.findViewById(R.id.checkbox);
        }

        @Override
        public void populateView(int position, final BookracksEntity item) {
            ImageLoader.disImage(context,item.getImg_url(),img);
            name.setText(item.getStory_name());
            dsc.setText(item.getChapter_name());
            data.setText(item.getCreated_at());
            sum_info.setText("总章节："+item.getChapter_num());
            if (isEdit){
                checkbox.setVisibility(View.VISIBLE);
                checkbox.setChecked(selectAll);
            }else {
                checkbox.setVisibility(View.GONE);
            }
            checkbox.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (checksList.contains(item.getId())){
                        checksList.remove(item.getId());
                    }else {
                        checksList.add(item.getId());
                    }
                    listener.changedNum(checksList.size()+"");
                }
            });
        }
    }

    public void setSelectAll(boolean isTrue){
        selectAll = isTrue;
        if (selectAll) {
            for (int i = 0; i < getCount(); i ++){
                if (!checksList.contains(getItem(i).getId())){
                    checksList.add(getItem(i).getId());
                }
            }
        }else {
            checksList.clear();
        }
        listener.changedNum(checksList.size()+"");
        notifyDataSetChanged();
    }

    public String getCheckedIds(){
        if (checksList.size() == 0) return null;
        StringBuffer stringBuffer = new StringBuffer("");
        for (int i = 0; i < checksList.size(); i ++){
            stringBuffer.append(checksList.get(i));
            if (i != checksList.size() - 1){
                stringBuffer.append(",");
            }
        }
        return stringBuffer.toString();
    }

}
